
<template>
  <div class="setpassword">
    <div class="top">
      <div class="top_left">
        <van-icon name="arrow-left" @click="$router.back()" />
      </div>
      <div class="top_center">设置密码</div>
      <div class="top_right">跳过</div>
    </div>
    <div class="form">
      <div class="form_top" :class="{ active: active == 1 }">
        <div class="top_left">
          <input
            type="text"
            placeholder="请输入手机号"
            @focus="active = 1"
            @blur="active = ''"
            v-model="form.mobile"
          />
        </div>
        <div class="top_right" @click="getCode">
          {{ codeFlag == false ? `获取验证码` : `获取验证码(${timer})` }}
        </div>
      </div>
      <div class="form_bom" :class="{ active: active == 3 }">
        <input
          type="text"
          v-model="form.password"
          placeholder="请输入密码"
          @focus="active = 3"
          @blur="active = ''"
        />
      </div>
      <div class="form_bom" :class="{ active: active == 2 }">
        <input
          type="text"
          v-model="form.sms_code"
          placeholder="请输入短信验证码"
          @focus="active = 2"
          @blur="active = ''"
        />
      </div>
    </div>
    <div class="bon">
      <button @click="add">提交</button>
    </div>
  </div>
</template>

<script>
import { setPassword, getsmsCode } from "@/request/longin";
// import { getsmsCode, getLogin } from "@/request/longin";
let reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
export default {
  name: "",
  components: {},
  data() {
    return {
      active: "",
      timer: 6,
      codeFlag: false,
      form: {
        mobile: "",
        sms_code: "",
        password: "",
        sms_type: "getPassword",
      },
    };
  },
  created() {
    // this.setPasswords();
  },
  mounted() {},
  methods: {
    // 获取验证码
    async getsmsCode() {
      let res = await getsmsCode(this.form);
      console.log(res);
    },
    getCode() {
      if (reg.test(this.form.mobile)) {
        this.getsmsCode();
        this.codeFlag = true;
        const timer = setInterval(() => {
          if (this.timer > 0) {
            this.timer--;
          } else {
            clearInterval(timer);
            this.codeFlag = false;
            this.timer = 6;
          }
        }, 1000);
        console.log(1);
      } else {
        console.log(2);
        Toast("手机号格式错误");
      }
    },
    async add() {
      let res = await setPassword(this.form);
      console.log(res);
      this.$router.push("/login");
    },
  },
};
</script>
<style scoped lang='scss'>
.setpassword {
  width: 100%;
  height: 100%;
  // background-color: #ccc;
  .top {
    width: 100%;
    height: 80px;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    box-sizing: border-box;
    align-items: center;
    background-color: #fff;
    margin-bottom: 10px;
  }
  .form {
    width: 100%;
    height: 100px;
    margin-top: 30px;
    // border: 1px solid #ccc;
    font-size: 20px;
    // background-color: red;
    margin-bottom: 20px;
    input {
      border: none;
      height: 35px;
    }
    .form_top {
      width: 70%;
      margin: 0 auto;
      display: flex;
      justify-content: space-between;
      align-items: center;
      // border-bottom: 1px solid #ccc;
    }
    .form_bom {
      width: 70%;
      // padding-left: 160px;
      margin: 0 auto;
      // border-bottom: 1px solid #ccc;
      margin-top: 30px;
      box-sizing: border-box;
    }
    .active {
      border-bottom: 2px solid #fd6411;
    }
  }
  .bon {
    background-color: #fff;
    width: 100%;
    // text-align: center;
    padding-left: 10px;
    box-sizing: border-box;
    margin-top: 100px;
    button {
      width: 90%;
      height: 80px;
      border-radius: 5px;
      border: none;
      background-color: #eb6100;
      color: #fff;
    }
  }
}
</style>
